package com.bdqn.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bdqn.entity.Student;
import com.bdqn.util.JdbcUtil;

public class StudentDaoImpl implements StudentDao{

	//增加数据的方法，传入学生参数，返回增加成功的数据条数，为int类型
	public int insert(Student student) {
		//先获取与数据库的连接
		Connection connection = JdbcUtil.getConnection();
		//输入SQL语句
		String sql="insert into stu(sname,spwd,gender,gid) values(?,?,?,?)";
		PreparedStatement statement=null;
		int rows=0;
		//通过conne获取语句对象
		try {
			statement = connection.prepareStatement(sql);
			//为语句中的？参数占位符进行赋值
			statement.setString(1, student.getSname());
			statement.setString(2, student.getSpwd());
			statement.setString(3, student.getGender());
			statement.setInt(4, student.getGid());
			//执行语句，返回增删改成功的行数
			rows = statement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(statement,connection);
		}
		return rows;
	}

	//修改数据
	public int update(Student student) {
		//先获取与数据库的连接
				Connection connection = JdbcUtil.getConnection();
				//输入SQL语句
				String sql="update stu set sname=?,spwd=?,gender=?,gid=? where sid=?";
				PreparedStatement statement=null;
				int rows=0;
				//通过conne获取语句对象
				try {
					statement = connection.prepareStatement(sql);
					//为语句中的？参数占位符进行赋值
					statement.setString(1, student.getSname());
					statement.setString(2, student.getSpwd());
					statement.setString(3, student.getGender());
					statement.setInt(4, student.getGid());
					statement.setInt(5, student.getSid());
					//执行语句，返回增删改成功的行数
					rows = statement.executeUpdate();
				} catch (SQLException e) {
					e.printStackTrace();
				}finally {
					JdbcUtil.close(statement,connection);
				}
				return rows;
	}

	//删除数据
	public int delete(int sid) {
		//先获取与数据库的连接
				Connection connection = JdbcUtil.getConnection();
				//输入SQL语句
				String sql="delete from stu where sid=?";
				PreparedStatement statement=null;
				int rows=0;
				//通过conne获取语句对象
				try {
					statement = connection.prepareStatement(sql);
					//为语句中的？参数占位符进行赋值
					statement.setInt(1,sid);
					//执行语句，返回增删改成功的行数
					rows = statement.executeUpdate();
				} catch (SQLException e) {
					e.printStackTrace();
				}finally {
					JdbcUtil.close(statement,connection);
				}
				return rows;
	}

	//查询数据
	public List<Student> selectAll() {
		List<Student> list=new ArrayList<Student>();
		Connection connection = JdbcUtil.getConnection();
		String sql="select * from stu";
		PreparedStatement statement=null;
		try {
			statement = connection.prepareStatement(sql);
			//执行查询，返回结果集
			ResultSet resultSet = statement.executeQuery();
			//对结果集遍历，并输出
			while(resultSet.next()) {
				int sid=resultSet.getInt(1);
				String sname=resultSet.getString(2);
				String spwd=resultSet.getString(3);
				String gender=resultSet.getString(4);
				int gid=resultSet.getInt(5);
				//将每一行记录转换为一个对象
				Student student = new Student(sid, sname, spwd, gender, gid);
				//将转换的对象传入集合中
				list.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}
	
	
	
	
	
	
	
	
}
